﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
public partial class LichTronGuiTram : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmLichTronGuiTram";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 6;
    static int CurrentPage = 1;
    static DataTable dtPrint;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;


                    Title = "Quản lý lịch trộn | " + Header;

                    btnCancel_Click(sender, e);

                   

                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                p = new SqlParameter[10];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@KhachHang", ex.GetGuid(drkhachhang.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@HangMuc",ex.GetGuid(drhangmuc.SelectedValue) );
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@GioTron", txtgiotron.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;

                p[5] = new SqlParameter("@MacBeTong", ex.GetGuid(drmacbetong.SelectedValue));
                p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                p[6] = new SqlParameter("@LoaiDa", drloaida.SelectedValue);
                p[6].SqlDbType = SqlDbType.Int;

                p[7] = new SqlParameter("@KhoiLuong", double.Parse(txtkhoiluong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkhoiluong.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@HinhThuc", ex.GetGuid(drhinhthuc.SelectedValue));
                p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                p[9] = new SqlParameter("@Del", 1);
                p[9].SqlDbType = SqlDbType.Int;

                ex.ExecuteNonQuerry("sp_XDALichTronGuiTram_Add", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tLịch Trộn\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        LoadMacBeTong(drmacbetong);
        LoadKhachHang(drkhachhang);
        LoadHangMuc(drhangmuc);
        LoadHinhThuc(drhinhthuc);
        txtngaythang.Text = "";
        txtgiotron.Text = "";
        txtkhoiluong.Text = "";
        LoadKhachHang(dlkhachhang);
        drkhachhang.SelectedIndex = 0;
        drhangmuc.SelectedIndex = 0;
        drhinhthuc.SelectedIndex = 0;
        drmacbetong.SelectedIndex = 0;
        drloaida.SelectedIndex = 0;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_XDALichTronGuiTram_List", p);
        GV.DataBind();
        btnSave.Visible = true;
        btncapnhat.Visible = false;

        btnFirst.Visible = true;
        btnNext.Visible = true;
        btnPre.Visible = true;
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdalichtronguitram_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                 //   LoadHangMuc(drhangmuc);
                    txtngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                    drkhachhang.SelectedValue = dt.Rows[0]["khachhang"].ToString();
                    LoadHangMucTheoKhachHang(drhangmuc);
                    drhangmuc.SelectedValue = dt.Rows[0]["hangmuc"].ToString();
                    drmacbetong.SelectedValue = dt.Rows[0]["macbetong"].ToString();
                    drloaida.SelectedValue = dt.Rows[0]["loaida"].ToString();
                    txtgiotron.Text = dt.Rows[0]["giotron"].ToString();
                    txtkhoiluong.Text = dt.Rows[0]["khoiluong"].ToString();
                    drhinhthuc.SelectedValue = dt.Rows[0]["hinhthuc"].ToString();
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tLịch trộn gửi trạm\t" + ax.Message);
                }
               
            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdalichtronguitram_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tLịch trộn\t" + ax.Message);

                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);
                p = new SqlParameter[10];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@KhachHang", ex.GetGuid(drkhachhang.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@HangMuc", ex.GetGuid(drhangmuc.SelectedValue));
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@GioTron", txtgiotron.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;

                p[5] = new SqlParameter("@MacBeTong", ex.GetGuid(drmacbetong.SelectedValue));
                p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                p[6] = new SqlParameter("@LoaiDa", drloaida.SelectedValue);
                p[6].SqlDbType = SqlDbType.Int;

                p[7] = new SqlParameter("@KhoiLuong", double.Parse(txtkhoiluong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkhoiluong.Text.Trim()));
                p[7].SqlDbType = SqlDbType.Float;
                p[8] = new SqlParameter("@HinhThuc", ex.GetGuid(drhinhthuc.SelectedValue));
                p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                p[9] = new SqlParameter("@Del", 1);
                p[9].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdalichtronguitram_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá ca bơm\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdalichtronguitram_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;

        GV.DataSource = ex.GetData("sp_xdalichtronguitram_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdalichtronguitram_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }

    void LoadKhachHang(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_ListKhachHang_LichTrong");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn khách hàng--", ""));
        dl.SelectedIndex = 0;
    }

    void LoadHangMucTheoKhachHang(DropDownList dl)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@KhachHang", ex.GetGuid(drkhachhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_ListHangMuc_LichTron_TheoKhachHang", p);
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn hạng mục--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadHangMuc(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_ListHangMuc_LichTron");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn hạng mục--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadMacBeTong(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_ListMacBeTong_LichTron");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn mác bê tông--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadHinhThuc(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_ListHinhThuc_LichTron");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn hình thức đổ bê tông--", ""));
        dl.SelectedIndex = 0;
    }
    
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        if (dlkhachhang.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(txtTuNgay.Text.Trim()));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(txtDenNgay.Text.Trim()));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            GV.DataSource = ex.GetData("[sp_xdalichtronguitram_List_TheoThoiGian]", p);
            GV.DataBind();
            btnFirst.Visible = false;
            btnNext.Visible = false;
            btnPre.Visible = false;
        }
        else {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(txtTuNgay.Text.Trim()));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(txtDenNgay.Text.Trim()));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@KhachHang",ex.GetGuid(dlkhachhang.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GV.DataSource = ex.GetData("sp_xdalichtronguitram_List_TheoThoiGian_KhachHang", p);
            GV.DataBind();
            btnFirst.Visible = false;
            btnNext.Visible = false;
            btnPre.Visible = false;
        }

    }
    protected void drkhachhang_SelectedIndexChanged(object sender, EventArgs e)
    {
        //p = new SqlParameter[1];
        //p[0] = new SqlParameter("@KH", ex.GetGuid(drkhachhang.SelectedValue));
        //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        //drCongTrinh.DataSource = ex.GetData("sp_CongTrinh_ListDrop_TheoKhachHang", p);
        //drCongTrinh.DataBind();
        //drCongTrinh.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
        //drCongTrinh.SelectedIndex = 0;
    }

    protected void drkhachhang_SelectedIndexChanged1(object sender, EventArgs e)
    {
        LoadHangMucTheoKhachHang(drhangmuc);
    }
    protected void txtDenNgay_TextChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(txtTuNgay.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(txtDenNgay.Text.Trim()));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        dlkhachhang.Items.Clear();
        dlkhachhang.DataSource = ex.GetData("sp_xdalichtronguitram_ListKhachHang", p);
        dlkhachhang.DataBind();
        dlkhachhang.Items.Insert(0, new ListItem("--Chọn khách hàng--", ""));
        dlkhachhang.SelectedIndex = 0;
    }
    protected void btnIn_Click(object sender, EventArgs e)
    {
        if (f_InBaoCao.Equals(1))
        {
           
            if (dlkhachhang.SelectedIndex == 0)
            {
                p = new SqlParameter[2];
                p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(txtTuNgay.Text.Trim()));
                p[0].SqlDbType = SqlDbType.SmallDateTime;
                p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(txtDenNgay.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;

                dtPrint = ex.GetData("sp_xdalichtronguitram_List_TheoThoiGian_Print", p);
            }
           
            PrintNPOI(dtPrint);
        }
        else
            gstGetMess("Bạn không có quyền in báo cáo này", "");
    }

    void PrintNPOI(System.Data.DataTable dt)
    {
        var workbook = new HSSFWorkbook();

        if (dt.Rows.Count > 0)
        {
            string sheetname = "Lịch trộn gửi trạm";

           

            var sheet = workbook.CreateSheet(sheetname);

            #region CSS
            var headerLabelCellStyle = workbook.CreateCellStyle();
            headerLabelCellStyle.Alignment = HorizontalAlignment.LEFT;
            headerLabelCellStyle.BorderBottom = CellBorderType.THIN;
            headerLabelCellStyle.BorderLeft = CellBorderType.THIN;
            headerLabelCellStyle.BorderRight = CellBorderType.THIN;
            headerLabelCellStyle.BorderTop = CellBorderType.THIN;

            var headerLabelFont = workbook.CreateFont();
            headerLabelFont.FontName = "Times New Roman";

            headerLabelFont.FontHeightInPoints = 11;
            headerLabelFont.Color = HSSFColor.BLACK.index;
            headerLabelCellStyle.SetFont(headerLabelFont);

            //--------------------------

            var hsRight = workbook.CreateCellStyle();
            hsRight.Alignment = HorizontalAlignment.RIGHT;
            hsRight.BorderBottom = CellBorderType.THIN;
            hsRight.BorderLeft = CellBorderType.THIN;
            hsRight.BorderRight = CellBorderType.THIN;
            hsRight.BorderTop = CellBorderType.THIN;

            hsRight.SetFont(headerLabelFont);
            //-------------------
            var hsCenter = workbook.CreateCellStyle();
            hsCenter.Alignment = HorizontalAlignment.CENTER;
            hsCenter.BorderBottom = CellBorderType.THIN;
            hsCenter.BorderLeft = CellBorderType.THIN;
            hsCenter.BorderRight = CellBorderType.THIN;
            hsCenter.BorderTop = CellBorderType.THIN;

            hsCenter.SetFont(headerLabelFont);
            //-----------------------

            var hs1 = workbook.CreateCellStyle();
            hs1.Alignment = HorizontalAlignment.LEFT;

            var hsb = workbook.CreateFont();
            hsb.Boldweight = (short)FontBoldWeight.BOLD;
            hsb.FontName = "Times New Roman";

            hsb.FontHeightInPoints = 11;
            hsb.Color = HSSFColor.BLACK.index;
            hs1.SetFont(hsb);

            //-------------
            var hs2 = workbook.CreateCellStyle();
            hs2.Alignment = HorizontalAlignment.CENTER;

            var hsb2 = workbook.CreateFont();
            hsb2.Boldweight = (short)FontBoldWeight.BOLD;
            hsb2.FontName = "Times New Roman";

            hsb2.FontHeightInPoints = 18;
            hsb2.Color = HSSFColor.BLACK.index;
            hs2.SetFont(hsb2);
            //------------------------------
            var hs3 = workbook.CreateCellStyle();
            hs3.Alignment = HorizontalAlignment.CENTER;
            hs3.BorderBottom = CellBorderType.THIN;
            var hsb3 = workbook.CreateFont();
            hsb3.Boldweight = (short)FontBoldWeight.BOLD;
            hsb3.FontName = "Times New Roman";

            hsb3.FontHeightInPoints = 14;
            hsb3.Color = HSSFColor.BLACK.index;
            hs3.SetFont(hsb3);

            //----------------------------
            var hs4 = workbook.CreateCellStyle();
            hs4.Alignment = HorizontalAlignment.CENTER;

            hs4.BorderBottom = CellBorderType.THIN;
            hs4.BorderLeft = CellBorderType.THIN;
            hs4.BorderRight = CellBorderType.THIN;
            hs4.BorderTop = CellBorderType.THIN;

            var hsb4 = workbook.CreateFont();
            hsb4.Boldweight = (short)FontBoldWeight.BOLD;
            hsb4.FontName = "Times New Roman";

            hsb4.FontHeightInPoints = 11;
            hsb4.Color = HSSFColor.BLACK.index;
            hs4.SetFont(hsb4);

            #endregion

            //Start header-----------------------------------------------------------------------
            #region Header
            //tao hnag dau tien
            var rowIndex = 0;

            //cong ty
            var row = sheet.CreateRow(rowIndex);
            Cell r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Công ty cổ phần Xây Dựng Đức Anh");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 4);
            sheet.AddMergedRegion(cra);

            //chi nhanh
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Chi nhánh bê tông Lâm Thao");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            cra = new CellRangeAddress(1, 1, 0, 4);
            sheet.AddMergedRegion(cra);

            //tieu de bao cao
           
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Lịch trộn gửi vận hành trạm trộn + thí nghiệm");
            r1c1.CellStyle = hs2;
            r1c1.Row.Height = 500;

            cra = new CellRangeAddress(2, 2, 0, 12);
            sheet.AddMergedRegion(cra);


            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Lâm Thao, ngày "+txtTuNgay.Text.Trim());
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

           
            //khachhang
            //rowIndex++;
            //row = sheet.CreateRow(rowIndex);
            //r1c1 = row.CreateCell(0);
            //r1c1.SetCellValue(dlKhachHang.SelectedIndex > 0 ? "Khách hàng: " + dlKhachHang.SelectedItem.Text + congtrinh : dlLoaiKH.SelectedIndex > 0 ? "Loại khách hàng: " + dlLoaiKH.SelectedItem.Text : "");
            //r1c1.CellStyle = hs3;
            //r1c1.Row.Height = 500;

         
            //freeze panes
          
            #endregion
            //End header-----------------------------------------------------------------------

            #region Header1-2

            //header2
            rowIndex++;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            string[] header = { "Khách Hàng", "Hạng mục", "Giờ trộn", "Mác bê tông", "Loại đá", "Khối lượng", "Hình thức đổ bê tông"
                              };

            var cell = row.CreateCell(0);
            cell.SetCellValue("STT");
            cell.CellStyle = hs4;
            cell.Row.Height = 400;


            //tao tieu de cot
            for (int j = 0; j < header.Length; j++)
            {
                cell = row.CreateCell(j+1);
                cell.SetCellValue(header[j].ToString());
                cell.CellStyle = hs4;
                cell.Row.Height = 500;

                sheet.AutoSizeColumn(j);

            }

            #endregion

            //ghi du lieu tung dong
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rowIndex++;
                row = sheet.CreateRow(rowIndex);
                cell = row.CreateCell(0);
                cell.SetCellValue((i + 1).ToString());
                cell.CellStyle = hsCenter;

                for (int j = 1; j < dt.Columns.Count; j++)
                {
                    cell = row.CreateCell(j);

                    if (j == 6)
                    {
                        if (!dt.Rows[i][j - 1].ToString().Trim().Equals(""))
                        {
                            cell.SetCellType(CellType.NUMERIC);
                            cell.SetCellValue(double.Parse(dt.Rows[i][j].ToString()));
                        }
                        else
                            cell.SetCellValue(dt.Rows[i][j].ToString());
                        cell.CellStyle = hsRight;
                    }
                    else
                    {
                        cell.SetCellValue(dt.Rows[i][j].ToString());

                        if (j <= 1)
                            cell.CellStyle = hsCenter;
                        else
                            cell.CellStyle = headerLabelCellStyle;
                    }
                    cell.Row.Height = 500;
                }

            }       

            for (int ik = 0; ik < 9; ik++)
            {
                sheet.AutoSizeColumn(ik);
            }

            rowIndex++;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Kế toán trạm trộn");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;
            //end sheet tong hop--------------------------
            using (var exportData = new MemoryStream())
            {
                workbook.Write(exportData);
                string filename = txtTuNgay.Text.Trim();
                string saveAsFileName = "LichTronGuiTram-" + filename + " " + txtTuNgay.Text.Trim() + ".xls";

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(exportData.GetBuffer());
                Response.End();
            }
        }
    }
}